package com.example.boot.rtshop.service.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.boot.rtshop.dao.RtShopDao;
import com.example.boot.rtshop.model.RtShop;
import com.example.boot.rtshop.service.RtShopService;
import com.example.boot.util.AddressMain;

@Service
public class RtShopImpl implements RtShopService {

	@Autowired
	private RtShopDao rtShopDao;

	@Override
	public List<RtShop> findByAllShop(String origin) throws Exception {
		List<RtShop> findByAllShop = rtShopDao.findByAllShop();
//		String query = "";
//		String url = "";
		for (RtShop r : findByAllShop) {
			// 店铺的距离
			String local = AddressMain.local(r.getShopAdress());

			String loadDistance = AddressMain.loadDistance(origin, local);
			r.setDistance(Integer.parseInt(loadDistance));
		}
		Collections.sort(findByAllShop, new Comparator<RtShop>() {
			@Override
			public int compare(RtShop o1, RtShop o2) {
				if (o1.getDistance() >= o2.getDistance()) {
					return 1;
				} else {
					return -1;
				}
			}
		});
		return findByAllShop;
	}

}
